package com.fowo.api.controller.shipment.fbainvoice;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.controller.purchase.vo.WarehouseBinParam;
import com.fowo.api.controller.purchase.vo.warehouseBinInventory;
import com.fowo.api.entity.WarehouseBin;
import com.fowo.api.mapper.WarehouseBinDMapper;
import com.fowo.api.model.fbainvoice.FBAInvoiceAllocateInventoryAction;
import com.fowo.api.model.fbainvoice.FBAInvoiceVo;
import com.fowo.api.model.fbainvoice.detail.FBAInvoiceDetailItemVo;
import com.fowo.api.service.impl.FBAInvoiceServiceImpl;
import com.fowo.api.service.impl.WarehouseBinServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component()
public class fbaInvoiceAllocateInventoryAction implements FBAInvoiceAllocateInventoryAction {
    @Resource
    private FBAInvoiceServiceImpl fbaInvoiceService;
    @Resource
    private WarehouseBinDMapper warehouseBinDMapper;
    @Resource
    private WarehouseBinServiceImpl warehouseBinService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void run(List<Long> idList) throws Exception {
        for(int i=0;i<idList.size();i++){
            allocateInventory(idList.get(i));
        }
    }
    void allocateInventory(Long id) throws Exception {
        List<Long> list=new ArrayList<>();
        list.add(id);
        FBAInvoiceVo voById = fbaInvoiceService.getVoById(id);
        //设置仓位
        setBin(voById);
        //发货状态改为待发货
        voById.setInvoiceStatus("30");
        fbaInvoiceService.update(voById);
        fbaInvoiceService.lockinventory(list);
        //修改为待发货状态

    }
    public void setBin(FBAInvoiceVo voById) throws Exception {
        List<FBAInvoiceDetailItemVo> invoiceDetailList = voById.getInvoiceDetailList();
        for(int i=0;i<invoiceDetailList.size();i++){
            FBAInvoiceDetailItemVo fbaInvoiceDetailItemVo = invoiceDetailList.get(i);
            WarehouseBinParam search=new WarehouseBinParam();
            search.setWarehouseId(voById.getDeliveryWarehouse());
            if(fbaInvoiceDetailItemVo.getShipmentShop()!=null){
                search.setShopId(Long.valueOf(fbaInvoiceDetailItemVo.getShipmentShop()));
            }
//            search.setFnskuName(fbaInvoiceDetailItemVo.getShipmentShopFnsku());
            if(fbaInvoiceDetailItemVo.getShipmentShopFnsku()!=null){
                String trim = fbaInvoiceDetailItemVo.getShipmentShopFnsku().trim();
                search.setFSku(ObjectUtils.isEmpty(trim)?null:trim);
            }else{
                search.setFSku(fbaInvoiceDetailItemVo.getShipmentShopFnsku());
            }

            search.setProductId(fbaInvoiceDetailItemVo.getName());
            List<warehouseBinInventory> wareBinOne = warehouseBinDMapper.getWareBinOne(search);
            if(wareBinOne.size()>0){
                warehouseBinInventory warehouseBinInventory = wareBinOne.get(0);
                fbaInvoiceDetailItemVo.setOutBingId(Long.valueOf(warehouseBinInventory.getId()));
                fbaInvoiceDetailItemVo.setWhbCode(warehouseBinInventory.getStorageBin());
            }else{
                WarehouseBin zancun = warehouseBinService.getOne(Wrappers.<WarehouseBin>query()
                        .lambda().eq(WarehouseBin::getStorageBin, "可用暂存")
                        .eq(WarehouseBin::getWarehouseId, voById.getDeliveryWarehouse()));
                fbaInvoiceDetailItemVo.setOutBingId(Long.valueOf(zancun.getId()));
                fbaInvoiceDetailItemVo.setWhbCode(zancun.getStorageBin());
            }
        }
        fbaInvoiceService.update(voById);
    }
}
